import java.sql.Timestamp;

import javax.swing.JFrame;

import com.mongodb.DBCursor;
import com.mongodb.DBObject;


public class Calculation {
	private ListOfPoints listOfPoints;
	private Dbase database;
	private int mul;
	private boolean end = false;
	private boolean firstRun;
	private Timestamp currentTime;
	private StopWatch stop = new StopWatch();
	private int count = 1;
	private int sleepTime;
	private Map map;
	private JFrame frame;
	private DBCursor cursor;
	private String[] colName;
	private int[][] sum;
	private int total;
	
	public Calculation(Dbase database, int mul, int sleepTime) {
		this.database = database;
		this.mul = mul;
		listOfPoints = new ListOfPoints(database.getListOfPointIndex(),
							database.getColName());
		//currentTime = database.getSoonestTime();
		//currentTime = new Timestamp(currentTime.getTime() + 0 * 60 * 60 * 1000);
		this.sleepTime = sleepTime;
		firstRun = true;
		cursor = database.test();
		colName = database.getColName();
		sum = new int[10][10];
		for(int i = 0; i < 10; i++) {
			for(int j = 0; j < 10; j++) {
				sum[i][j] = 0;
			}
		}
		total = 0;
	}
	
	/*
	 * To do: this method will visualize the map
	 */
	public void run() {
		while(!end) {
			System.out.println("------------------------------------------------------------------------");
			end = checkEnd();
			if(end) break;
			stop.start();
			updateList();
			updateMap();
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//currentTime = new Timestamp(currentTime.getTime() + mul * 60000);
			stop.stop();
			System.out.println("");
			System.out.println("Time to run session " + count + ": "
					+ stop.getElapsedTime() + " ms");
			System.out.println("------------------------------------------------------------------------");
			System.out.println("");
			count++;
		}
	}
	
	private void createMap() {
		frame = new JFrame("Map");
	  	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    map = new Map(database.getMinLatitude(), database.getMaxLatitude(),
	    				database.getMinLongtitude(), database.getMaxLongtitude());
	    frame.add(map);
	    frame.setSize(map.getWidth(), map.getHeight());
	    frame.setResizable(false);
	    frame.setLocation(50, 50);
	    frame.setVisible(true);
	}
	
	/*
	 * To do: update Map
	 */
	private void updateMap() {
		if (firstRun) {
			createMap();
			firstRun = false;
		}
		map.setList(listOfPoints);
		map.repaint();
		frame.repaint();
	}
	
	/*
	 * To do: update list of Points
	 */
	private void updateList() {
		//DBCursor cursor = database.getObject(currentTime);
		StopWatch stop2 = new StopWatch();
		stop2.start();
		//listOfPoints.setLocationOfList(cursor);
		for(int i = 0; i < mul; i++) {
			DBObject temp = cursor.next();
			if(temp != null) {
				/*double y = ((Double) temp.get(colName[1]) - database.getMinLatitude()) / (database.getMaxLatitude() - database.getMinLatitude()) * 10.0;
				double x = ((Double) temp.get(colName[2]) - database.getMinLongtitude()) / (database.getMaxLongtitude() - database.getMinLongtitude()) * 10.0;
				sum[(int) x][(int) y]++;
				total++;*/
				//System.out.println(temp.get(colName[0]).getClass() + " " + temp.get(colName[1]).getClass() + " " + temp.get(colName[2]).getClass());
					listOfPoints.setLocation((Integer) temp.get(colName[0]),
							new Double(temp.get(colName[1]).toString()),
							new Double(temp.get(colName[2]).toString()));
				
			} else {
				end = true;
				return;
			}
		}
		stop2.stop();
		//listOfPoints.printList();
		/*for(int i = 0; i < 10; i++) {
			for(int j = 0; j < 10; j++) {
				System.out.print(sum[i][j] + " ");
			}
			System.out.println();
		}*/
		System.out.println(stop2.getElapsedTime());
	}
	
	/* check whether end of calculation */
	private boolean checkEnd() {
		return (cursor.next() == null);
	}
}

